layui.use(['jquery','element','form','layedit'],function () {
    var $ = layui.$
        ,element = layui.element
        ,form = layui.form
        ,layedit = layui.layedit;

    //校验登录状态
    if(isNotLogIn()){
        return;
    }

    var serviceId = getUrlParms("serviceId");
    //获取服务
    var index = layer.load(3);
    $.ajax({
        url : "/CivilAffairs/manager/getServiceInfo",
        type : "post",
        data:{"serviceType":"2"},
        async : false,
        dataType : "json",
        success : function(result) {
            layer.close(index);
            if(checkResult(result)){
                $(".service-div").html("<label class=\"layui-form-label\">服务类型</label>"+result.data);
            }
            form.render('select');
        },
        error:function(result){
            layer.close(index);
            reqError(result,"获取服务类别异常");
        }
    });
    //创建一个编辑器
    //设置图片上传接口,现业务中暂不支持该增加图片
    layedit.set({
        uploadImage: {
            url: '/CivilAffairs/news/uploadServicePic' //接口url
            ,type: 'post' //默认post
        }}
    );
    var serviceIndex = layedit.build('serviceContent');
    var optionIndex = layedit.build('optionContent');

    //如果是编辑页面,从后台获取新闻的内容
    if(!isNull(serviceId)){
        var index = layer.load(3);
        $.ajax({
            url:"/CivilAffairs/manager/getDataInfoById",
            type:"post",
            data:{"dataId":serviceId,"dataType":"serviceOption"},
            dataType:"json",
            success:function(result){
                layer.close(index);
                if(checkResult(result)){
                    var serviceInfo = result.data[0];
                    $("#optionName").val(serviceInfo.OPTION_NAME);
                    //失效的新闻展示为未生效
                    if("0"==serviceInfo.VALID_TAG){
                        var isValid = $("#isValid");
                        isValid.removeAttr("checked");
                    }
                    $("#services").val(serviceInfo.SERVICE_ID);
                    //给提交按钮赋值新闻的itemid
                    $("#addService").attr("itemid",serviceInfo.OPTION_ID);
                    form.render();
                    $("#serviceContent").val(serviceInfo.SERVICE_CONTENT);
                    $("#optionContent").val(serviceInfo.OPTION_CONTENT);
                    serviceIndex = layedit.build("serviceContent");
                    optionIndex = layedit.build("optionContent");
                }
            },
            error : function(result) {
                layer.close(index);
                console.log("获取服务信息异常:"+result.status+","+result.statusMessage+",beneficiaryId:"+beneficiaryId);
                layer.msg("系统异常,请稍后重试",{icon:2});
            }
        });
    }
    //form提交时校验,用来将layedit中的内容同步到textarea中
    form.verify({
        serviceContent: function(value) {
            return layedit.sync(serviceIndex);
        },
        optionContent: function(value) {
            return layedit.sync(optionIndex);
        }
    });

    //添加&修改新闻的提交
    form.on("submit(addService)",function(data){
        //校验登录状态
        if(isNotLogIn()){
            return false;
        }
        var optionId = $(this).attr("itemid");
        var url = "";
        var reqData = "";
        var managerId = getCookie("managerId");
        //是添加新闻
        if(isNull(optionId)){
            optionId = null;
        }
        //提交修改
        var index = layer.load(3);
        $.ajax({
            url:"/CivilAffairs/manager/modifyServiceOption",
            type:"post",
            data:{"serviceInfo":JSON.stringify(data.field),"optionId":optionId,"managerId":managerId},
            dataType:"json",
            success:function(result){
                layer.close(index);
                if(checkResult(result)){
                    layer.msg(result.detail,{icon:6});
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                }
            },
            error : function(result) {
                layer.close(index);
                console.log("获取、修改服务信息出错:"+result.status+","+result.statusMessage+",beneficiaryId:"+beneficiaryId);
                layer.msg("系统异常,请稍后重试",{icon:2});
            }
        });
        return false;
    });




});